/**
 * @参数详情 obj 接收字符串的对象
 * @参数详情 key 接收字符串的键
 * @参数详情 minLength 最小长度
 * @参数详情 maxLength 最大长度
 * @参数详情 allowNull 是否允许本身是空值
 */
class StringVar {
    constructor(obj, key, allowNull = false, minLength = null, maxLength = null) {
        this.value = obj[key];
        this.key = key;
        this.allowNull = allowNull;
        this.minLength = minLength;
        this.maxLength = maxLength;
    }

    check() {
        if (!this.allowNull && !this.value) {
            return { success: false, message: `${this.key} 不能为空` };
        }
        this.value = String(this.value || '');
        if (this.minLength && this.value.length < this.minLength) {
            return { success: false, message: `${this.key} 最小长度为 ${this.minLength}` };
        }
        if (this.maxLength && this.value.length > this.maxLength) {
            return { success: false, message: `${this.key} 最大长度为 ${this.maxLength}` };
        }
        return { success: true };
    }
}

module.exports = StringVar;